const express = require('express');
const expressWS = require('express-ws');
const router = express.Router();
expressWS(router);

// ws://localhost:8080/ws/libai
// ws://localhost:8080/ws/zhangsan


let resData = [];
router.ws('/zhangsan', ws => {
    ws.onmessage = (e) => {
        resData.push(e.data);
        ws.send(e.data);
        console.log(e.data);
    }
})

router.ws('/lisi1', ws => {
    setInterval(() => {
        if(resData.length > 0) {
            let msg = resData[0];
            resData.shift();
            ws.send(msg);
        } 
    }, 200)
})






let resData1 = [];
router.ws('/lisi', ws => {
    ws.onmessage = (e) => {
        ws.send(e.data);
        resData1.push(e.data); 
        console.log(e.data);
    }
})
router.ws('/zhangsan1', ws => {
    setInterval(() => {
        if(resData1.length > 0) {
            let msg = resData1[0];
            resData1.shift();
            ws.send(msg);
        } 
    }, 200 )
})






module.exports = router;